import { Controller, Post, Inject, Body } from '@midwayjs/core';
import { AuthService } from '../service/auth.service';
import { LoginDto, RegisterDto } from '../dto/userDto';
import { Validate } from '@midwayjs/validate';

@Controller('/api/auth')
export class AuthController {
    @Inject()
    authService: AuthService;

    @Post('/login')
    @Validate()
    async login(@Body() loginDto: LoginDto) {
        try {
            const result = await this.authService.login(loginDto);
            return result;
        } catch (error) {
            return { code: 400, message: error.message, data: null };
        }
    }
    @Post('/register')
    @Validate()
    async register(@Body() registerDto: RegisterDto) {
        try {
            const result = await this.authService.register(registerDto);
            return { code: 200, message: '注册成功', data: result };
        } catch (error) {
            return { code: 400, message: error.message, data: null };
        }
    }
}
